<!--
* @author: lzm
* @date: 2023年12月8日 15:04:10
* @description：选择物料弹窗
* @update: 2023年12月8日 15:04:10
-->
<template>
  <div>
    <el-dialog
      :title='title'
      :visible='showDialog'
      width='910px'
      center
      top='5vh'
      custom-class='my-dialog'
      @close='close'
      :close-on-click-modal='false'
      :close-on-press-escape='false'
      append-to-body>
      <div class='inventory-top form-top'>
        <el-form ref='materialForm' :model='materialForm' label-width='100px'>
          <el-row :gutter='24'>
            <el-col :span='8'>
              <el-form-item label='物料名称'>
                <el-select
                  filterable
                  style='width: 100%'
                  v-model='materialForm.name'
                  placeholder='请选择物料名称'>
                  <el-option
                    v-for='(item, index) in materialName'
                    :key='index'
                    :label='item'
                    :value='item'
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span='8'>
              <el-form-item label='规格型号'>
                <el-select
                  filterable
                  style='width: 100%'
                  v-model='materialForm.model'
                  placeholder='请选择规格型号'
                >
                  <el-option
                    v-for='(item, index) in standardsList'
                    :key='index'
                    :label='item'
                    :value='item'
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span='8' style='text-align: right'>
              <el-button type='primary' class='chamfer-98' @click='queryList'>
                搜索
              </el-button>
              <el-button
                style='margin-left: 20px'
                class='chamfer-98'
                plain
                @click='resetBtn'
              >
                重置
              </el-button>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div class='table-div table-normal'>
        <el-table
          v-loading='loading'
          element-loading-background='rgba(0, 0, 0, 0.6)'
          :data='materialTable'
          fit
          style='width: 100%'
          header-row-class-name='tableHeader'
          header-cell-class-name='tableTrHeader'
          row-class-name='tableRow'
          cell-class-name='tableCell'
          @selection-change='handleSelectionChange'
          @row-click="clickRow"
          ref='materialTables'
          show-checkbox
        >
          <el-table-column
            align='center'
            type='selection'
            width='100'
            label-class-name='DisabledSelection'
          />
          <el-table-column
            type='index'
            label='序号'
            width='100'
            align='center'
          />
          <el-table-column prop='name' label='物料名称' align='center' />
          <el-table-column prop='model' label='规格型号' align='center' />
          <el-table-column prop='singleWeight' label='单重(g)' align='center'>
            <template slot-scope='scope'>
              {{ scope.row.singleWeight }}g
            </template>
          </el-table-column>
          <el-table-column prop='' label='操作' align='center'>
            <template v-slot='{ row }'>
              <el-button type='text' @click='configurationBtn(row)'>
                配置单重
<!--                <i v-if='row.isConfigComplete' class='el-icon-check el-icon&#45;&#45;right'></i>-->
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <paging-component
          layout='total, prev, pager, next, jumper'
          :pager-count='5'
          :total='total'
          :page-size='materialForm.currentSize'
          :current-page='materialForm.currentPage'
          @handleSizeChange='handleSizeChange'
          @handleCurrentChange='handleCurrentChange'
        />
      </div>
      <div slot='footer' class='dialog-footer'>
        <el-button type='primary' class='chamfer-98' @click='confirmBtn'>确认</el-button>
      </div>
    </el-dialog>

    <single-configuration @configComplete='configComplete' :logisticsId='logisticsId' :position-id='positionId' :merge-count="mergeCount" :ws-data='wsData' :show-dialog.sync='showConfig' />
  </div>
</template>
<script>
import {
  getMaterialList,
  getMaterialNameList,
  getMaterialModelList
} from '@/api/materialManage'
import { updateSingleWeightStatus,bindSave} from '@/api/locatorManage'
import pagingComponent from '@/components/PagingComponent/index.vue'
import SingleConfiguration from './singleConfiguration.vue'

export default {
  components: { pagingComponent, SingleConfiguration },
  props: {
    // 是否显示弹窗
    showDialog: {
      type: Boolean,
      default: false
    },
    //  弹窗标题
    title: {
      type: String,
      default: '选择物料'
    },
    // 当前要绑定物流的库位数据
    currentLocationData: {
      type: Object,
      default: () => {
        return {}
      }
    },
    // ws推送的数据
    wsData: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },

  data() {
    return {
      // 物料表单
      materialForm: {
        name: '', // 物料名称
        model: '', // 规格型号
        currentPage: 1, // 页码
        currentSize: 20 // 页尺寸
      },
      total: 0, // 总条数
      materialTable: [], // 物料列表
      materialName: [], // 物料名称下拉列表
      standardsList: [], // 物料规格下拉列表
      selectData: [], // 选中的数据
      showConfig: false, // 配置单重弹窗
      logisticsId: null,// 物料id
      loading: false, // 加载
      positionId: '', // 当前库位id
      mergeCount: '', // 当前库位合并数量
    }
  },
  watch: {
    currentLocationData: {
      handler(val) {
        this.positionId = val.id
        this.mergeCount = val.mergeCount
      },
      deep: true
    },
    showDialog: {
      handler(val) {
        if (val) {
          this.selectData = [];
          this.$nextTick(() => {
            // 清除表格选中状态
            this.$refs.materialTables.clearSelection();
          });
        }
      },
      deep: true
    },
  },
  mounted() {
    this.renderList()
    this.getDictData()
  },
  methods: {
    clickRow(row){//单击行选中前面复选框
      this.$refs.materialTables.toggleRowSelection(row);
    },
    queryList() {
      this.materialForm.currentPage = 1
      this.renderList()
    },
    async renderList() {
      this.loading = true
      const { result } = await getMaterialList(this.materialForm)
      this.total = Number(result.totalCount)
      // isConfigComplete 是否配置单重
      this.materialTable = result.currentData
      this.loading = false
    },
    // 获取字典数据
    async getDictData() {
      // 更新下拉菜单数据
      let { result: list1 } = await getMaterialNameList()
      this.materialName = list1
      let { result: list2 } = await getMaterialModelList()
      this.standardsList = list2
    },
    // 选中数据
    handleSelectionChange(val) {
      this.selectData = val
    },
    // 确认
    confirmBtn() {
      if (this.selectData.length > 1 || this.selectData.length === 0) {
        this.$message({
          type: 'warning',
          message: '请选择最多一个物料进行绑定',
          showClose: true
        })
        return
      }
      if (this.selectData[0].singleWeight === 0) {
        this.$message({
          type: 'warning',
          message: '该物料未配置单重，请先配置单重',
          showClose: true
        })
        return
      }
      bindSave({
        positionId: this.positionId,
        goodsId: this.selectData[0].id
      }).then(res => {
        if (res.status === '2000') {
          this.$message({
            type: 'success',
            message: '绑定成功'
          })
          this.$emit('update:showDialog', false)
          this.$emit('bindComplete')
        }
      })
    },
    // 重置
    resetBtn() {
      this.materialForm = {
        name: '', // 物料名称
        model: '', // 规格型号
        currentPage: 1, // 页码
        currentSize: 20 // 页尺寸
      }
      this.renderList()
    },
    // 关闭弹窗清空所有值
    close() {
      this.$emit('update:showDialog', false)
      this.materialForm = {
        name: '', // 物料名称
        model: '', // 规格型号
        currentPage: 1, // 页码
        currentSize: 20 // 页尺寸
      }
    },
    // 配置单重
    configurationBtn(row) {
      this.logisticsId = row.id
      updateSingleWeightStatus({
        id: this.currentLocationData.id,
        status: true,
      }).then(res => {
        if (res.status === '2000') {
          this.showConfig = true
        }
      })
    },
    // 单重配置完成
    configComplete(positionId) {
      // console.log('configComplete',positionId)
      // this.materialTable.forEach(item=>{
      //   if(item.id === positionId){
      //     item.isConfigComplete = true
      //   }
      // })
      // this.showConfig = false
      this.renderList()
    },
    // pageSize改变时会触发(每页数据条数)
    handleSizeChange(val) {
      this.materialForm.currentSize = val
      this.renderList()
    },
    // currentPage改变时会触发(当前页码数)
    handleCurrentChange(val) {
      this.materialForm.currentPage = val
      this.renderList()
    }
  }
}
</script>
<style scoped lang='scss'>
@import '@/styles/_handle.scss';

.inventory-top {
  margin-top: 20px;

  .el-form-item {
    margin-bottom: 0;
  }

}

.chamfer-98 {
  font-size: 18px;
}

.table-div {
  min-height: 500px;
}

.el-row {
  text-align: center;
}

// 弹窗样式
::v-deep .el-dialog--center .el-dialog__body {
  padding: 0px 25px 30px;
}

// 表格样式
::v-deep .el-table {
  margin-top: 12px;
  background: transparent;
  border: none;
  // 表头行样式
  .tableHeader {
    height: 48px;
    -webkit-clip-path: polygon(
        10px 0,
        calc(100% - 10px) 0,
        100% 15%,
        100% 85%,
        calc(100% - 10px) 100%,
        10px 100%,
        0 85%,
        0 15%
    );
    clip-path: polygon(
        10px 0,
        calc(100% - 10px) 0,
        100% 15%,
        100% 85%,
        calc(100% - 10px) 100%,
        10px 100%,
        0 85%,
        0 15%
    );
  }

  // 表头单元格样式
  .tableTrHeader {
    background-color: transparent;
    border: none;
    font-weight: 400;
    color: #fff;
    font-size: 18px;
  }

  // 表格行样式
  .tableRow {
    background-color: transparent;
    border: none;
  }

  // 表格单元格样式
  .tableCell {
    background: transparent;
    border: none;
    color: #fff;
    font-size: 18px;
    font-weight: 400;
  }
}

// 去除表头底部横线
::v-deep .el-table th.el-table__cell.is-leaf,
.el-table td.el-table__cell {
  border: none;
}

// 取消表格底部横线
::v-deep .el-table--border::after,
.el-table--group::after,
.el-table::before {
  content: none;
}

::v-deep .el-table__body-wrapper {
  max-height: 50vh;
  overflow-y: auto;
}

::v-deep .el-table__body-wrapper::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

// 配置单重按钮样式
.el-button--text {
  font-size: 16px;
  color: #fff;
  font-weight: 700;
}

.el-icon-check {
  font-size: 16px;
  color: #1afa01;
  font-weight: 700;
}
</style>
